package org.jetlinks.community.mstn.task;

import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.logging.events.AccessLoggerAfterEvent;
import org.hswebframework.web.logging.events.AccessLoggerBeforeEvent;

import org.jetlinks.community.mstn.events.MstnAssetsHandlerAfterEvent;
import org.jetlinks.community.mstn.events.MstnAssetsHandlerBeforeEvent;
import org.jetlinks.community.rule.engine.executor.MstnFuncTaskExecutorProvider;
import org.reactivestreams.Publisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Slf4j
@Component("mstnTask")
public class TestTask {


    @EventListener
    public void handleEvent(MstnFuncTaskExecutorProvider.MstnFuncExecutorConfig event){
        event.async(this.doSomething(event));
    }

    @EventListener
    public void handleBeforEvent(AccessLoggerBeforeEvent event){
        log.info("before:{}",event.getLogger().getContext().get("userId"));
    }
    @EventListener
    public void handleAssetsBeforeEvent(MstnAssetsHandlerBeforeEvent event){
        log.info("before assets");
    }

    @EventListener
    public void handleAssetsAfterEvent(MstnAssetsHandlerAfterEvent event){
        log.info("after assets");
    }

    @EventListener
    public void handleAfterEvent(AccessLoggerAfterEvent event){
//        event.getLogger().getContext().forEach((key, value) -> log.info("key:{},value:{}", key, value));
//        log.info("class:{}描述:{}",event.getLogger().getTarget(),event.getLogger().getDescribe());
//        Tenant tenantClass = event.getLogger().getTarget().getAnnotation(Tenant.class);
//        if (tenantClass != null) {
//            Tenant tenantMethod = event.getLogger().getMethod().getAnnotation(Tenant.class);
//            if (tenantMethod != null) {
//                String userId = event.getLogger().getContext().get("userId");
//            }
//        }
    }

    private Publisher<?> doSomething(MstnFuncTaskExecutorProvider.MstnFuncExecutorConfig event) {
        log.info("test{}",event.getFuncName());
        return Mono.just("test");
    }

    public void test(String l){
        log.info("========test:{}",l);
    }
}
